//
//  VCSettingViewController.m
//  VCardPro
//
//  Created by Nguyen Thong Thai on 9/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VCSettingViewController.h"

#define kVCRecognizedLanguage @"recognized_language"

@interface VCSettingViewController () 

- (void)loadSettingInfos;
- (void)registerDefaultsFromSettingsBundle;
- (void)configureView;

@end

@implementation VCSettingViewController
@synthesize languageCellLabel;
@synthesize languageLabel;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        settingData = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController setTitle:NSLocalizedString(@"Settings", nil)];
    [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"Back", nil)];
    // Load user settings
    [self loadSettingInfos];
    [self configureView];
}

- (void)viewDidUnload
{
    [self setLanguageCellLabel:nil];
    [self setLanguageLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
	
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - instance methods

- (void)loadSettingInfos
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSString *language = [defaults valueForKey:kVCRecognizedLanguage];
	if (!language) {
		// If the default value doesn't exist then we need to manually set them.
		[self registerDefaultsFromSettingsBundle];
		language = [[NSUserDefaults standardUserDefaults] stringForKey:kVCRecognizedLanguage];
	}
    if (language) {
        if (!settingData) {
            settingData = [[NSMutableDictionary alloc] init];
        }
        [settingData setValue:language forKey:kVCRecognizedLanguage];
    }
}

- (void)registerDefaultsFromSettingsBundle 
{
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if (!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
        return;
    }
    
    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
    
    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if (key) {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
        }
    }
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
}

- (void)configureView
{
    if (self) {
        [self.languageLabel setText:[settingData valueForKey:kVCRecognizedLanguage]];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"LanguageSettingSegue"]) {
        UINavigationController *navigationController = (UINavigationController *)[segue destinationViewController];
        VCLanguageSettingViewController *settingLanguageVC = (VCLanguageSettingViewController *)navigationController.topViewController;
        settingLanguageVC.delegate = self;
        [settingLanguageVC setCurrentLanguage:[settingData valueForKey:kVCRecognizedLanguage]];
    }
}

#pragma mark - VCLanguageSettingViewControllerDelegate

- (void)VCLanguageSettingViewControllerDidSave:(VCLanguageSettingViewController *)controller didChooseLanguage:(NSString *)language
{
    [settingData setValue:language forKey:kVCRecognizedLanguage];
    [self.languageLabel setText:language];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setValue:language forKey:kVCRecognizedLanguage];
    [userDefault synchronize];
    
    [self dismissModalViewControllerAnimated:YES];
}

@end
